window.onload=trackDiimoData();

function trackDiimoData(){	
	// The goal page for submitting data
	
	function getFromDiimoUrlVars() {
	    var vars = {};
	    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
	        vars[key] = value;
	    });
	    return vars;
	}

	function loadDiimoScript(){

		var diimoScript = document.createElement('script');
		diimoScript.setAttribute('src', 'http://www.demot.diimosolutions.com/clothingforus/js/diimoTracker.js');
		// diimoScript.setAttribute('src', 'http://localhost/clothingforus/js/diimoTracker.js');
		diimoScript.async = true;
		
		document.getElementsByTagName('head')[0].appendChild(diimoScript);

		return diimoScript;
		
	}

	var affiliateKey = getFromDiimoUrlVars()["diimoAffiliateKey"];

	var urlGoal="TrackerTest";  

	// we are at the landing page from click. Now ceate the cookie on this site aswell. 
	if(typeof affiliateKey !== 'undefined' || affiliateKey!=null){
		
		loadDiimoScript().onload = function() {

			createDiimoCookie('clickedFromDiimoEvent',affiliateKey,1);

			// alert('Cookie');
			
		};

	}

	if((window.location.href.indexOf(urlGoal) > -1)) {

			loadDiimoScript().onload = function() {

				var requestValues=readDiimoCookie('clickedFromDiimoEvent');

				if(typeof requestValues !== 'undefined' || requestValues!=null){

					callDiimoDomain(requestValues);
					
					 _gaq.push(['_trackEvent',
					            'Diimo Event Fired', // category of activity
					            'User in shopping cart', // Action
					         ]);

					// eraseDiimoCookie('clickedFromDiimoEvent');

					// alert(trackingKeys);
					
				}
	  	  		  
		    };

	}

}